package com.ljy.my_study.java.时间轮;

import lombok.Data;

/**
 * @description:
 * @author: James
 * @create: 2022-04-16 19:28
 */
public class Task implements Runnable{

    private Integer taskId;

    private Integer nextTime;

    private Runnable runnable;

    private TimeRoundMonitor monitor;

    public Task(Integer taskId,Integer nextTime,Runnable runnable,TimeRoundMonitor timeRoundMonitor){
        this.taskId=taskId;
        this.nextTime=nextTime;
        this.runnable=runnable;
        this.monitor=timeRoundMonitor;
    }

    @Override
    public void run() {
        preProcess();
        doRun();
    }

    protected void doRun(){
        runnable.run();
    }

    /**
     * 后续任务加入处理，实际上会根据cron表达式来判断是否需要加入处理，这里简化处理，直接使用cycle
     */
    private void preProcess(){
        if(TimeRoundMonitor.needAdd()){
            this.monitor.add(new Task(this.taskId,this.nextTime,this.runnable,this.monitor));
        }
    }

    public Integer getNextTime() {
        return nextTime;
    }

    public Integer getTaskId() {
        return taskId;
    }

    public void setTaskId(Integer taskId) {
        this.taskId = taskId;
    }

    public void setNextTime(Integer nextTime) {
        this.nextTime = nextTime;
    }

    public Runnable getRunnable() {
        return runnable;
    }

    public void setRunnable(Runnable runnable) {
        this.runnable = runnable;
    }

    public TimeRoundMonitor getMonitor() {
        return monitor;
    }

    public void setMonitor(TimeRoundMonitor monitor) {
        this.monitor = monitor;
    }
}
